package com.qiche.controller.mini;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.PageResult;
import com.qiche.aspect.Result;
import com.qiche.dto.SimpleMemberDto;
import com.qiche.enums.ResultEnum;
import com.qiche.model.StoreMemberCar;
import com.qiche.model.User;
import com.qiche.service.StoreMemberCarService;
import com.qiche.service.StoreMemberService;
import com.qiche.util.UserUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chendezhi
 * @date 2018/3/29 17:14
 *
 * 门店用户 小程序
 */
@RestController
@RequestMapping("/user/mini")
public class UserMiniController {

    @Autowired
    private StoreMemberService storeMemberService;
    @Autowired
    private StoreMemberCarService storeMemberCarService;


    /**
     * 根据关键字(手机号码,姓名,车牌号码) 模糊搜索用户
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "根据关键字(手机号码,姓名,车牌号码) 模糊搜索用户")
    @GetMapping("/likeSearch/member")
    public PageResult searchCustomerByPhoneOrNameOrCarnum(@RequestParam(name = "keyword",required = false) String keyword,
                                                    @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        User currentUser = UserUtils.getCurrentUser();
        if (currentUser == null) {
            return new PageResult(ResultEnum.MEMBER_LOGIN_PAST);
        }
        PageInfo<SimpleMemberDto> pageInfo = storeMemberService.searchByPhoneOrNameOrCarNum(keyword, currentUser, pageNum,pageSize);
        PageResult<List<SimpleMemberDto>> listPageResult = new PageResult<>(ResultEnum.SUCCESS, pageInfo.getList());
        BeanUtils.copyProperties(pageInfo, listPageResult);
        return listPageResult;
    }

}
